var Page = function(){
    this.init();
};
Page.prototype = {
    params: {
        passport: '',
        helpId:'',
        _isEdit:0,
    },
    init: function(){
        this.initParams();
        this.initEvent();
    },
    initParams: function(){
        this.params.passport = Pub.getCookie('_PASSPORT');
        this.params.helpId = Pub.getSession('helpId');
        this.params._isEdit = Pub.getSession('_isEdit');
    },
    getData: function(){
        var _this = this;
        var paramsObj = {
            passport: _this.params.passport,
            helpId:parseInt(_this.params.helpId),
        };
        Api.newbusinesshelplistdetails(paramsObj, function(data){
            console.log(data);
            _this.getDataList(data.list)
            var data=data.data;
            $('#title').val(data.title);
            $('#describe').val(data.describe);
            $('#classifySelect').val(data.className);
            $('#address').val(data.address);
            var ue = UE.getEditor('container');
            ue.ready(function() {//编辑器初始化完成再赋值
              ue.setContent(data.content);  //赋值给UEditor
            });
            $('#name').val(data.realName);
            $('#mobile').val(data.mobile);
            if (data.isRecommend==0) {
                honeySwitch.showOff("#isRecommend")
            };


        });
    },
    getDataList: function(data) {
        var _this = this;
        if (data && data.length > 0) {
            var html = '',
                i = 0,
                len = data.length;
            for (i; i < len; i++) {
                html += '<tr>' +

                    '<td class="text-c">' + data[i].realName + '</td>' +
                    '<td class="text-c">' + data[i].gmtCreate + '</td>'+
                    '<td class="text-c">' + data[i].content + '</td>'+

                    '</tr>';
            }
            $('#List').html(html);


        } else {
            var html = '<tr>' +
                '<td class="text-c c-666" colspan="3" >暂无数据</td>' +
                '</tr>';
            $('#List').html(html);

        }
    },
    isState:function(){
        var _this = this;
        if (_this.params._isEdit==0) {
                $('#pro-title').text('添加');
            }
        $('.btDiv').on('click', 'div', function(event) {
            event.preventDefault();
            if ($(this).attr('data')==2) {
                window.location.href = Pub.getHtmlUrl('./business-helps-list.html');
            }else{
                if ($("#isRecommend").hasClass('switch-on')) {
                    var isRecommend=1;
                }else{
                    var isRecommend=0;
                };
                var content = ue.getContent();
                var paramsObj = {
                    passport: _this.params.passport,
                    helpId:parseInt(_this.params.helpId),
                    title:$('#title').val(),
                    classId:$('#classifySelect').val(),
                    className:$('#classifySelect').find("option:selected").text(),
                    describe:$('#describe').val(),
                    address:$('#address').val(),
                    realName:$('#name').val(),
                    mobile  :$('#mobile ').val(),
                    isRecommend:isRecommend,
                    content:content
                };
                var Obj = {
                    passport: _this.params.passport,
                    title:$('#title').val(),
                    classId:$('#classifySelect').val(),
                    className:$('#classifySelect').find("option:selected").text(),
                    describe:$('#describe').val(),
                    address:$('#address').val(),
                    mobile  :$('#mobile ').val(),
                    realName:$('#name').val(),
                    isRecommend:isRecommend,
                    content:content
                };
                console.log(paramsObj);
                if (_this.params._isEdit==1) {
                    Api.newbusinesshelplistmodify(paramsObj, function(data){
                        console.log(data);
                        layui.use('layer', function(){
                            layer.msg('提交成功', {icon: 6});
                            setTimeout(function(){
                                window.location.href = Pub.getHtmlUrl('./business-helps-list.html');
                            },1500)
                        });
                    });
                }else{
                    Api.newbusinesshelplistadd(Obj, function(data){
                        console.log(data);
                        layui.use('layer', function(){
                            layer.msg('提交成功', {icon: 6});
                            setTimeout(function(){
                                window.location.href = Pub.getHtmlUrl('./business-helps-list.html');
                            },1500)
                        });
                    });
                };
            };


        });
    },
    otherEvent:function(){
        var _this = this;
        $('.goods-checkbox input').iCheck({
            checkboxClass: 'icheckbox-blue',
            radioClass: 'iradio-blue',
            increaseArea: '20%'
        })
        $('#money').keyup(function(){
            var c=$(this);
            if(/[^\d]/.test(c.val())){//替换非数字字符
              var temp_amount=c.val().replace(/[^\d]/g,'');
              $(this).val(temp_amount);
            }
        })
    },
    programclasslistget:function(){
        var _this=this;
        var paramsObj = {
            passport: _this.params.passport,
            type:4
        };
        Api.newpulldown(paramsObj, function(data){
            console.log(data);
            _this.programclasslist(data.data)
        })
    },
    programclasslist:function(data){
        var _this=this;
        if (data && data.length > 0) {
            var html = '<option value="" selected>请选择分类</option>', i = 0, len = data.length;
            for (i; i < len; i++) {
                html+='<option value="'+data[i].classId+'">'+data[i].className+'</option>';
            }
            $('#classifySelect').html(html);
            _this.programclasslistEvent();

        }
    },
    programclasslistEvent:function(){
        var _this=this;
        $('#classifySelect').on('change', function(event) {
            event.preventDefault();
            _this.params.classId=$(this).val();
            _this.params.className=$(this).find("option:selected").text();
        });
    },
    initEvent: function(){
        var _this = this;
        if (_this.params._isEdit==1) {
            setTimeout(function(){
                _this.getData();
            },500)
        };

        _this.otherEvent();
        _this.isState();
        _this.programclasslistget();


    }
};
var page = new Page();